package com.ys.util;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

public class ScreenValueMergeStrategy extends AbstractMergeStrategy {

    /**
     *  第几个表格
     */
    private Integer exportNum;

    public ScreenValueMergeStrategy() {
    }

    /**
     * @param exportNum      第几个表格
     */
    public ScreenValueMergeStrategy(Integer exportNum) {
        this.exportNum = exportNum;
    }

    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {

        if (relativeRowIndex != null){
            if(cell.getColumnIndex() >=1 && cell.getColumnIndex()<=19){
                if (relativeRowIndex %2 != 0){
                    CellStyle style = sheet.getWorkbook().createCellStyle();
                    if(cell.getColumnIndex()==19){
                        style.setBorderRight(BorderStyle.THIN);	//设置单元格右边框的边框类型
                        style.setRightBorderColor(IndexedColors.BLACK.getIndex());	//设置右边边框的颜色
                    }
                    if(cell.getColumnIndex()==1){
                        style.setBorderLeft(BorderStyle.THIN);	//设置单元格左边框的边框类型
                        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());	//设置左边框的颜色
                    }
                    if(cell.getColumnIndex()>=7){
                        style.setAlignment(HorizontalAlignment.CENTER);	//设置单元格的水平对齐类型（居中）
                    }
                    style.setVerticalAlignment(VerticalAlignment.CENTER);	//设置单元格的垂直对齐类型（居中）
                    style.setBorderBottom(BorderStyle.DASHED);	//设置单元格下边框使用的边框类型
                    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());	//设置下边框的颜色

                    Font dataFont = sheet.getWorkbook().createFont();
                    dataFont.setFontName("楷体_GB2312");	//设置字体的名称(例如:仿宋)
                    dataFont.setFontHeightInPoints((short) 9);	//设置字体高度
                    style.setFont(dataFont);

                    Row dataRow = sheet.getRow(21+relativeRowIndex);
                    //Cell dataCell1 = dataRow.getCell(cell.getColumnIndex());
                    Cell dataCell1 = dataRow.createCell(cell.getColumnIndex());
                    dataCell1.setCellValue(cell.getStringCellValue());
                    dataCell1.setCellStyle(style);
                }

            }


            if (cell.getColumnIndex() == 1){
                if (relativeRowIndex %2 != 0){
                    CellRangeAddress cellRangeAddress1 = new CellRangeAddress(21+relativeRowIndex,21+relativeRowIndex,1,7);
                    sheet.addMergedRegionUnsafe(cellRangeAddress1);
                }
            }

        }
    }
}
